// pages/bianjidizhi/bianjidizhi.js
const app = getApp()
var ea = require('../../utils/each.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: ['四川省', '成都市', '锦江区'],
    customItem: '全部',
    getEditressUrl: app.data.url + '/api/my/editaddress',
    getAddressUrl: app.data.url + '/api/my/getadd',
    id:'',
    shuju:[],

  },

  // 获取页面所有input框中的值
  inputWacth: function (e) {
    let item = e.currentTarget.dataset.model;
    var shuju = this.data.shuju;
    shuju[item] = e.detail.value;
    // console.log(shuju);
    this.setData({ shuju: shuju });
    this.setData({
      [item]: e.detail.value
    });
  },
  update: function () {
    console.log(1)
  },

  //修改地址成功，
  xiugai: function () {
    // console.log(e);
    // return
    var that = this;
    var shuju = that.data.shuju;
    var user = wx.getStorageSync('user');
    console.log(shuju)
    wx.request({
      url: that.data.getEditressUrl,
      data: {token:user.token,shuju:shuju},
      method: 'post',
      success: function (res) {
        var data = res.data;
        console.log(data);
        // return;
        if (data.code == 200) {
          wx.showToast({
            title: '修改成功',
            icon: 'success'
          })
          setTimeout(function () {
            wx.navigateBack({
              delta: 1,
            })
          }, 1000)
        } else {
          wx.showToast({
            title: data.msg,
            icon: 'none'
          })
        }
      }
    })

  },


  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var that = this;
      var id = '';
        if (options.id) {
          var id = options.id;
          that.setData({ id: id });
        }
        this.getaddress(id);
      },
      getaddress:function(id){
        var that = this;
        var user = wx.getStorageSync('user');
        var tiaojian = {user:user}
        if (id) {
          tiaojian.id = id
        }
        // console.log(tiaojian);
        wx.request({
          url: that.data.getAddressUrl,
          data: { token: user.token,id:id},
          method:'post',
          success: function (res) {
            var data = res.data;
            console.log(data);
            if (data.code == 200) {
              if (data.data) {
                that.setData({ shuju: data.data});
                var region = [];
                if (data.data.province) {
                  region.push(data.data.province);
                }
                if (data.data.city) {
                  region.push(data.data.city);
                }
                if (data.data.area) {
                  region.push(data.data.area);
                }
                that.setData({ region: region });
              }
              // console.log(data)
              var shuju = data.data;
              that.setData({ shuju: shuju });
            } else {
              wx.showToast({
                title: data.msg,
                icon: 'none'
              })
            }
          }
        })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var user = wx.getStorageSync('user');
    var titles = wx.getStorageSync('titles');
    return {
      title: titles,
      path: '/pages/index/index?scene' + user.id,
    };
  }
})